iT邦幫忙

8

蠻可愛的 golang #5

  • 分享至 

  • xImage
  •  

今天介紹 const 以及 iota

package main

import (
	"fmt"
)

const two = 2

const (
	Female = 2
	Male   = 1
)

const (
	a = iota
	b = iota
	c = iota
)

const (
	Sunday = iota
	Monday
	Tuesday
)

func main() {
	fmt.Printf("two = %d\n", two)
	fmt.Printf("Female is %d\n", Female)
	fmt.Printf("Male is %d\n", Male)
	fmt.Printf("a = %d b = %d c = %d\n", a, b, c)
	fmt.Printf("Sunday = %d Monday = %d Tuesday = %d\n", Sunday, Monday, Tuesday)
}

執行結果如下:

two = 2
Female is 2
Male is 1
a = 0 b = 1 c = 2
Sunday = 0 Monday = 1 Tuesday = 2

可以比較方便,少打一些字.


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
總裁
iT邦好手 1 級 ‧ 2014-07-04 13:10:12

const (
a = iota
b = iota
c = iota
)

const (
Sunday = iota
Monday
Tuesday
)

這一段請解釋清楚,這禮拜其它天跑哪去了....毆飛

0
一級屠豬士
iT邦大師 1 級 ‧ 2014-07-04 13:35:36

改用中文版補上.

<pre class="c" name="code">
package main

import (
	"fmt"
)

const two = 2

const (
	Female = 2
	Male   = 1
)

const (
	a = iota
	b = iota
	c = iota
)

const (
	Sunday = iota
	Monday
	Tuesday
)

const (
	星期日 = iota
	星期一
	星期二
	星期三
	星期四
	星期五
	星期六
)

func main() {
	fmt.Printf("two = %d\n", two)
	fmt.Printf("Female is %d\n", Female)
	fmt.Printf("Male is %d\n", Male)
	fmt.Printf("a = %d b = %d c = %d\n", a, b, c)
	fmt.Printf("Sunday = %d Monday = %d Tuesday = %d\n", Sunday, Monday, Tuesday)
	fmt.Println()
	fmt.Printf("星期日 = %d 星期一 = %d 星期二 = %d\n", 星期日, 星期一, 星期二)
	fmt.Printf("星期日 = %d 星期一 = %d 星期二 = %d\n", 星期三, 星期四, 星期五)
	fmt.Printf("星期六 = %d", 星期六)
}

執行結果如下:

我要留言

立即登入留言